class CMainEditor {
	 private down1:down
	 private left1:leftTree
	 private right1:right
	 private up1:up
	 private medim:mainScence
	 private stage:egret.Stage
	 private newplatte1:newplatte
     private CurrentGoal:COBJECT
     private namecount:number=0
	 static myself:CMainEditor
	 private ROTAswitch:boolean=false
	 private objectname:string
	 private saveoropenordelete:SAVEorOPENorDELETE
	private constructor() {
	   
		this.stage=egret.MainContext.instance.stage
	    
		
	}
	public get OBJECTNAME ():string
	{
		return this.objectname
	}
	public set OBJECTNAME(A:string)
	{
		this.objectname=A;
	}
    public get isROTA():boolean
	{
		return this.ROTAswitch
	}
	public set isROTA(a:boolean)
	{
		this.ROTAswitch=a
	}
	public readresjason(a:any)
	{
      this.down1.readjason(a)
	}
	public static get editor():CMainEditor
	{
		if(this.myself==undefined)this.myself=new CMainEditor
		return this.myself
	}
	public loadingALL()
	{
		let datas=CBASE_MANAGE.getCBASE_MANAGE.getALLobject();
		for(let a=0;a<datas.length;a++)
		{   
			this.medim.loadingALL(datas[a]);
			this.left1.addlist(datas[a].objname,datas[a].resname);
		}
	}
	public clearALL()
	{
        CMainEditor.editor.goal=undefined;
		CCollitionEditor.editor.goal=undefined;
		CCollitionEditor.editor.chackchange();
		CObjectEditor.editor.chackchange();
		this.left1.clearALL();
		this.medim.clearALL();
	
	}
	public  showup()
	{
		this.down1=new down
		this.left1=new leftTree
		this.right1=new right
        this.up1=new up
		this.medim=new mainScence
	
		this.stage.addChild(this.down1)
		this.stage.addChild(this.left1)
		this.stage.addChild(this.right1)
		
		this.stage.addChild(this.up1)
		
		this.stage.addChild(this.medim)
			
		this.up1.x=0,this.up1.y=0
        this.left1.x=0,this.left1.y=this.up1.height
        this.medim.x=this.left1.width+10,this.medim.y=this.up1.height
        this.right1.x=this.medim.x+this.medim.width+10,this.right1.y=this.up1.height
        this.down1.x=15,this.down1.y=this.left1.y+this.left1.height+20;
		
        	var timer:egret.Timer = new egret.Timer(100);
		    timer.addEventListener(egret.TimerEvent.TIMER,()=>{this.right1.update()},this);
		    timer.start()
		
		
	}
	public get goal():COBJECT
	{
		
		return this.CurrentGoal
	}
	public set goal(c:COBJECT)
	{
	    this.cancel_new_object()
		this.CurrentGoal=c
	    this.rightupdate();
		this.right1.movieupdata();
		this.right1.scriptlistupdata();
		CObjectEditor.editor.update();
		
		
	}
	public rightupdate()
	{
      	this.right1.clear();
		this.right1.update();
	}
	public rightchangeTOcollition()
	{
		this.right1.changetoCollition();
	}
	public rightchangeTOcobject()
	{
		this.right1.changetoCobject();
	}
	public newmedimplatte(x?:number,y?:number,color?:number)
	{
		if(x==undefined)
		{
		 if(this.newplatte1==undefined)
         this.newplatte1=new newplatte;
		 this.stage.addChild(this.newplatte1)
		 this.newplatte1.x=this.stage.stageWidth/2-this.newplatte1.width/2
		 this.newplatte1.y=this.stage.stageHeight/2-this.newplatte1.height/2
		 this.down1.touchEnabled=false
		 this.up1.touchEnabled=false
		 this.medim.touchEnabled=false
		 this.left1.touchEnabled=false
		}
		else 
		{
			this.clearALL();
			this.medim.setplatte(x,y,color)
		    this.cancelnewmedimplatte()
		}
	}
	public cancelnewmedimplatte()
	{
        this.stage.removeChild(this.newplatte1)
		this.down1.touchEnabled=true
		this.up1.touchEnabled=true
		this.medim.touchEnabled=true
		this.left1.touchEnabled=true
	}
	public addmedim(x:number,y:number)
	{
      
	   this.cancel_new_object()
	   this.medim.addchild(x,y)
	   CObjectEditor.editor.update();
	}
	
	public get getgoalAt():number
	{
		return this.medim.getgoalAt(this.CurrentGoal)
	}
	public addnewbitmapOBJECTtostage(a:egret.Texture,x:number,y:number,resname:string)
	{
	
		this.cancel_new_object()
		let name="obj"+CBASE_MANAGE.getCBASE_MANAGE.namecount++;
		
		let someone=new COBJECT(name);
		someone.addbitmapBytextrue(a);
	    
		this.goal=someone
		this.stage.addChild(this.CurrentGoal);
		
		this.namecount++;
		this.CurrentGoal.x=x;
		this.CurrentGoal.y=y;
		this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE,this.movenewgoal,this);
		this.goal.resnmae=resname
	}
	public addnewmovieclickOBJECTtostage(a:string,x:number,y:number,resname:string)
	{
	
		this.cancel_new_object()
		let name="obj"+CBASE_MANAGE.getCBASE_MANAGE.namecount++;
		
		let someone=new COBJECT(name);
		someone.addMovieClickByname(a);
	    
		this.goal=someone
		this.stage.addChild(this.CurrentGoal);
		
		this.namecount++;
		this.CurrentGoal.x=x;
		this.CurrentGoal.y=y;
		this.stage.addEventListener(egret.TouchEvent.TOUCH_MOVE,this.movenewgoal,this);
		this.goal.resnmae=resname
	}
	private  movenewgoal(e:egret.TouchEvent)
	{
		
        this.CurrentGoal.x=e.stageX
	    this.CurrentGoal.y=e.stageY
	}
	public cancel_new_object()
	{
		
       if(this.stage.getChildIndex(this.CurrentGoal)!=-1)
		{
		  this.stage.removeEventListener(egret.TouchEvent.TOUCH_MOVE,this.movenewgoal,this);
          this.stage.removeChild(this.CurrentGoal)
		}
	}
	public get CurrentGoalstagexy():egret.Point
	{
		if(this.medim.getgoalAt(this.CurrentGoal)==-1)
		{
			let errpos=new egret.Point(0,0)
			return errpos;
		}
        return this.medim.stagexy(this.CurrentGoal.x,this.CurrentGoal.y);
	}
	
	public changeGoalIndex(index:number)
	{
		this.medim.changeGoalIndex(index)
	}
	public addtoleft()
	{
		this.left1.addlist(this.goal.objname,this.goal.resnmae)
	}
	public objnamechange(prvname:string,nowname:string)
	{
		this.left1.objnamechange(prvname,nowname);
	}
	public collitionnamechange()
	{
       this.left1.collisionnamechange();
	}
	public changetreeselectByobjname(name:string)
	{
		this.left1.changeseletByobjectname(name)
	}
	public changetreeselectBycollition(target:CCOLLISION)
	{
		this.left1.changeseletBycollision(target);
	}
	public objectchangetocollition()
	{
        CObjectEditor.editor.chackchange();
		CCollitionEditor.editor.state=true;
		CBASE_MANAGE.getCBASE_MANAGE.Allcollitiontouchenabel_forCollitionEditor();
		CCollitionEditor.editor.update();
	}
	public collitionchangetoobject()
	{
        CCollitionEditor.editor.chackchange();
		CCollitionEditor.editor.state=false;
		CBASE_MANAGE.getCBASE_MANAGE.Allcollitiontouchenabel_forOvjectEditor();
		CObjectEditor.editor.update();
	}
    public objctdeletes()
	{
		this.medim.removeCURRENtarget()
		this.left1.removelist(this.CurrentGoal.objname)
		this.CurrentGoal.deletemyself()
		this.CurrentGoal=undefined;
		CObjectEditor.editor.update();
	}
	public rungamebegin()
	{
		this.medim.stoplisten();
		this.up1.stoplisten();
		this.right1.stoplisten();
		this.left1.stoplisten();
		this.down1.stoplisten();
		CCollitionEditor.editor.chackchange();
		CObjectEditor.editor.chackchange();
		ASSIST.getASSIST.setplatte(this.medim.platte);
		
	}
	public rungameend()
	{
		this.medim.beginlisten();
		this.up1.beginlisten();
		this.right1.beginlisten();
		this.left1.beginlisten();
		this.down1.beginlisten();
		CCollitionEditor.editor.update();
		CObjectEditor.editor.update();
		CBASE_MANAGE.getCBASE_MANAGE.Onend();
	}
	public opensaveopendelete(mode:saveopendeletemode)
	{
		if(this.saveoropenordelete==undefined)this.saveoropenordelete=new SAVEorOPENorDELETE
		 this.stage.addChild(this.saveoropenordelete)
		 this.saveoropenordelete.pushdate();
		 this.saveoropenordelete.setmode(mode);
		 this.saveoropenordelete.x=this.stage.stageWidth/2-this.saveoropenordelete.width/2
		 this.saveoropenordelete.y=this.stage.stageHeight/2-this.saveoropenordelete.height/2
	}
	public closesaveopendelete()
	{
		if(this.stage.getChildIndex(this.saveoropenordelete)!=-1)
		{
		  this.stage.removeChild(this.saveoropenordelete)
		}
	}
	public getplattedata():number[]
	{
		return this.medim.getplattedata()
	}
	public loadingplatte(a:any)
	{
		this.medim.setplatte(a.plattewidth,a.platteheight,a.plattecolor);
	}
	public setplattetoASSIST()
	{
		ASSIST.getASSIST.setplatte(this.medim.platte);
	}
}